home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BPNN133U.ZIP / NNCREAT.C < prev    next >
C/C++ Source or Header  |  1992-11-19  |  5KB  |  250 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:    nncreat.c
  4. *    desc:    create a fully connected neural net
  5. *    by:    patrick ko
  6. *    date:    v1.1u - 02 aug 91
  7. *    revi:    v1.2b - 15 jan 92, adaptive coefficients (beta)
  8. *        v1.3u - 17 jan 92, revised data structures
  9. *-----------------------------------------------------------------------------
  10. */
  11. #include    <stdio.h>
  12. #ifdef        __TURBOC__
  13. #include    <stdarg.h>
  14. #include    <alloc.h>
  15. #endif
  16.  
  17. #include    "nntype.h"
  18. #include    "nnerror.h"
  19. #include    "nncreat.h"
  20. #include    "random.h"
  21.  
  22. REAL    UB=1.5;
  23. REAL    LB=0.5;
  24.  
  25. /*
  26. *=============================================================================
  27. *    funct:    nn_creat
  28. *    dscpt:    creat an nn object
  29. *    given:    totallayer = total number of hidden and output layers
  30. *        diminput = dimension of inputlayer
  31. *        dimother = dimension of other layers
  32. *    retrn:    allocated NET
  33. *=============================================================================
  34. */
  35. NET * nn_creat( totallayer, diminput, dimoutput, dimother )
  36. INTEGER        totallayer;
  37. INTEGER        diminput;
  38. INTEGER     dimoutput;
  39. INTEGER        * dimother;
  40. {
  41.     INTEGER    i;
  42.     INTEGER    dimwt;
  43.     NET    *nntmp;
  44.     INTEGER    dimlayer;
  45.  
  46.     /* malloc the NET struct */
  47.  
  48.     if ((nntmp= (NET *)malloc(sizeof(NET))) == NULL)
  49.         {
  50.         error( NNMALLOC );
  51.         }
  52.     else
  53.         {
  54.  
  55.         /* malloc the LAYER *'s    */
  56.  
  57.         DimNet(nntmp) = totallayer;
  58.         if ((nntmp->layer = (LAYER **)malloc(sizeof(LAYER *) * DimNet(nntmp))) == NULL)
  59.             {
  60.             error( NNMALLOC );
  61.             }
  62.         else
  63.             {
  64.             /*
  65.             * dimension of first layer's wgt vector
  66.             * is equal to dimension of input vector
  67.             */
  68.             dimwt = diminput;
  69.  
  70.             for (i=0; i<DimNet(nntmp); i++)
  71.                 {
  72.                 if (i == DimNet(nntmp)-1)
  73.                     {
  74.                     dimlayer = dimoutput;
  75.                     }
  76.                 else
  77.                     {
  78.                     dimlayer = *(dimother + i);
  79.                     }
  80.  
  81.                 Layer(nntmp,i) = l_creat( dimlayer, dimwt );
  82.  
  83.                 /*
  84.                 * dimension of this layer's wgt vector is equal
  85.                 * to dimension of previous layer's out vector
  86.                 */
  87.                 dimwt = dimlayer;
  88.                 }
  89.  
  90.             return (nntmp);
  91.             }
  92.         }
  93. }
  94.  
  95. /*
  96. *=============================================================================
  97. *    funct:    v_creat 
  98. *    dscpt:    create an allocated VECTOR object
  99. *    given:    dim = dimension of the vector
  100. *    retrn:    allocated VECTOR
  101. *=============================================================================
  102. */
  103. VECTOR * v_creat( dim )
  104. INTEGER    dim;
  105. {
  106.     VECTOR    *vtmp;
  107.     INTEGER    i;
  108.  
  109.     if ((vtmp = (VECTOR *)malloc(sizeof(VECTOR))) == NULL)
  110.         {
  111.         error( NNMALLOC );
  112.         }
  113.     else
  114.         {
  115.         DimVect(vtmp) = dim;
  116.         if ((Vect(vtmp) = (REAL *)malloc(sizeof(REAL) * dim)) == NULL)
  117.             {
  118.             error( NNMALLOC );
  119.             }
  120.         else
  121.             {
  122.             v_fill(vtmp, 0.0);
  123.             return (vtmp);
  124.             }
  125.         }
  126. }
  127.  
  128. /*
  129. *=============================================================================
  130. *    funct:    v_fill 
  131. *    dscpt:    fill all dim of a vector with a value
  132. *    given:    v = vector, m = value
  133. *    retrn:    v
  134. *=============================================================================
  135. */
  136. VECTOR * v_fill( v, m )
  137. VECTOR    *v;
  138. REAL    m;
  139. {
  140.     int    i;
  141.  
  142.     for (i=0; i<DimVect(v); i++)
  143.         {
  144.         Vi(v,i) = m;
  145.         }
  146.     return (v);
  147. }
  148.  
  149. /*
  150. *=============================================================================
  151. *    funct:    v_rand
  152. *    dscpt:    fill a vector with random value (default 0.5 - 1.5)
  153. *    given:    v = vector
  154. *    retrn:    v
  155. *=============================================================================
  156. */
  157. VECTOR *v_rand( v )
  158. VECTOR    *v;
  159. {
  160.     INTEGER    i;
  161.  
  162.     for (i=0; i<DimVect(v); i++)
  163.         {
  164.         Vi(v,i) = rnd() * (UB-LB) + LB;
  165.         }
  166.     return (v);
  167. }
  168.  
  169.  
  170. /*
  171. *=============================================================================
  172. *    funct:    u_creat
  173. *    dscpt:    create an allocated UNIT
  174. *    given:    dimwgtvect = dimension of the unit's weight vector
  175. *    retrn:    allocated UNIT
  176. *=============================================================================
  177. */
  178. UNIT * u_creat( dimwgtvect )
  179. INTEGER    dimwgtvect;
  180. {
  181.     UNIT    *utmp;
  182.  
  183.     if ((utmp=(UNIT *)malloc(sizeof(UNIT))) == NULL)
  184.         {
  185.         error(NNMALLOC);
  186.         }
  187.     else
  188.         {
  189.         vWeight(utmp) = v_creat( dimwgtvect );
  190.         v_rand( vWeight(utmp) );
  191.         vdWeight1(utmp) = v_creat( dimwgtvect );
  192.         vdWeight2(utmp) = v_creat( dimwgtvect );
  193.         vDO(utmp) = v_creat( dimwgtvect );
  194.  
  195.         Out(utmp) = 0.0;
  196.         Net(utmp) = 0.0;
  197.         Dlt(utmp) = 0.0;
  198.         nDlt(utmp)= 0.0;
  199.         Bias(utmp) = rnd() / 5000.0 ;
  200.         dBias1(utmp) = 0.0;
  201.         dBias2(utmp) = 0.0;
  202.  
  203.         /* allocation successful */
  204.         return (utmp);
  205.         }
  206. }
  207.  
  208.  
  209.  
  210. /*
  211. *=============================================================================
  212. *    funct:    l_creat
  213. *    dscpt:    create an allocated LAYER object
  214. *    given:    dimlayer = number of units in this layer
  215. *        dimwgtvect = dimension of the weight vector of each unit
  216. *    retrn:    allocated LAYER
  217. *=============================================================================
  218. */
  219. LAYER * l_creat( dimlayer, dimwgtvect )
  220. INTEGER    dimlayer;
  221. INTEGER    dimwgtvect;
  222. {
  223.     LAYER    *ltmp;
  224.     INTEGER    i;
  225.  
  226.     if ((ltmp= (LAYER *)malloc(sizeof(LAYER))) == NULL)
  227.         {
  228.         error(NNMALLOC);
  229.         }
  230.     else
  231.         {
  232.         DimLayer(ltmp) = dimlayer;
  233.         if ((ltmp->unit= (UNIT **)malloc(sizeof(UNIT *) * dimlayer)) == NULL)
  234.             {
  235.             error(NNMALLOC);
  236.             }
  237.         else
  238.             {
  239.             for (i=0; i<dimlayer; i++)
  240.                 {
  241.                 Unit(ltmp,i) = u_creat( dimwgtvect );
  242.                 }
  243.  
  244.             /* allocation successful */
  245.             return (ltmp);
  246.             }
  247.  
  248.         }
  249. }
  250.